package com.zzy.other;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * leecode 146
 * LRU cache by using LinkedHashMap
 * @param <K> K
 * @param <V> V
 * @author zzy
 */
public class L146LruLinkedHashMap<K,V> extends LinkedHashMap<K,V>{

    private int capacity;

    public L146LruLinkedHashMap(int initialCapacity) {
        super(initialCapacity, 0.75f, true);
        this.capacity = initialCapacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        L146LruLinkedHashMap<Integer, Integer> map = new L146LruLinkedHashMap<>(3);
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);
        System.out.println(map.keySet());

        map.put(4,4);
        System.out.println(map.keySet());

        map.put(3,3);
        System.out.println(map.keySet());
        map.put(3,3);
        map.put(3,3);
        map.put(5,5);
        System.out.println(map.keySet());

        map.put(6,6);
        System.out.println(map.keySet());
    }
}
//true 看上去true的更符合lru的预期
//[1, 2, 3]
//[2, 3, 4]
//[2, 4, 3]
//[4, 3, 5]
//[3, 5, 6]

//false
//[1, 2, 3]
//[2, 3, 4]
//[2, 3, 4]
//[3, 4, 5]
//[4, 5, 6]

